﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MayhemWpf.UserControls;
using Microsoft.Win32;

namespace ComputerMayhem.Wpf
{
    /// <summary>
    /// Interaction logic for UnreadEmailCountConfig.xaml
    /// </summary>
    public partial class UnreadEmailCountConfig : WpfConfiguration
    {

        public override string Title
        {
            get
            {
                return "Unread Email Count";
            }
        }

        public string EmailToCheck
        {
            get;
            set;
        }

        public int NumberOfUnreadEmails
        {
            get;
            set;
        }

        public UnreadEmailCountConfig(string email, int count)
        {
            InitializeComponent();

            //Initialize email accounts
            RegistryKey HKLM = Registry.CurrentUser;
            RegistryKey RegKey = HKLM.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\UnreadMail");

            string[] subKeys = RegKey.GetSubKeyNames();

            foreach (var item in subKeys)
            {
                this.EmailsComboBox.Items.Add(item.ToString());
            }

            this.EmailsComboBox.SelectedIndex = this.EmailsComboBox.Items.IndexOf(email);

            EmailToCheck = email;
            NumberOfUnreadEmails = count;
        }

        public override void OnLoad()
        {
            EmailNumberBox.Text = NumberOfUnreadEmails.ToString();
        }

        public override void OnSave()
        {
            int number;
            bool parse = int.TryParse(EmailNumberBox.Text, out number);

            string selectedEmail = this.EmailsComboBox.SelectedValue.ToString();

            if (parse && !string.IsNullOrEmpty(selectedEmail))
            {
                NumberOfUnreadEmails = number;
                EmailToCheck = selectedEmail;
            }
        }

        private void EmailNumberBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            CanSave = true;
            int level;
            bool parse = int.TryParse(EmailNumberBox.Text, out level);

            if (!(EmailNumberBox.Text.Length > 0 && parse && level >= 0 && level <= 100))
            {
                CanSave = false;
            }

            textInvalid.Visibility = CanSave ? Visibility.Collapsed : Visibility.Visible;
        }

        private void EmailsComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CanSave = true;
            if (string.IsNullOrEmpty(this.EmailsComboBox.SelectedValue.ToString()))
            {
                CanSave = false;
            }
        }
    }
}
